package org.mall.modules.seller.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.mall.common.api.vo.CommonResult;
import org.mall.modules.message.mapper.AppMessageMapper;
import org.mall.modules.pojo.shop.ShopDTO;
import org.mall.modules.seller.entity.Seller;
import org.mall.modules.seller.entity.SellerVo;
import org.mall.modules.seller.mapper.SellerMapper;
import org.mall.modules.seller.service.ISellerService;
import org.mall.modules.shop.service.ShopService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @Description: 商户/店员
 * @Author: jeecg-boot
 * @Date:   2019-10-23
 * @Version: V1.0
 */
@Service
public class SellerServiceImpl extends ServiceImpl<SellerMapper, Seller> implements ISellerService {

    @Resource
    private SellerMapper sellerMapper;
    @Resource
    private AppMessageMapper messageMapper;

    @Resource
    private ShopService shopService;

    /**
     * 商户/店员通过手机号查询
     *
     * @param mobile
     * @return
     */
    @Override
    public Seller getOneByMobile(String mobile) {
        return sellerMapper.getByMobile(mobile);
    }

    /**
     * 商户/店员通过用户名查询
     *
     * @param username
     * @return
     */
    @Override
    public Seller getOneByUserName(String username) {
        return sellerMapper.getByUserName(username);
    }

    /**
     * 查询该用户是否已添加过
     *
     * @param mobile
     * @return
     */
    @Override
    public Seller getOneUser(String mobile) {
        return sellerMapper.getUser(mobile);
    }

    /**
     * 删除用户后，二次添加
     *
     * @param id
     */
    @Override
    public void recoverOneUser(String id) {
        sellerMapper.recoverOneUser(id);
    }

    /**
     * 商户删除一个店员
     *
     * @param id
     */
    @Override
    public void deleteOneById(String id) {
        sellerMapper.deleteById(id);
    }

    /**
     * 商户批量删除店员
     *
     * @param asList
     */
    @Override
    public void deleteListById(List<String> asList) {
        sellerMapper.deleteByIdS(asList);
    }

    /**
     * 新增一个店主与店员的关系
     *
     * @param id
     * @param clerkId
     * @return
     */
    @Override
    public boolean insertOneClerkRelationShip(String id, String clerkId) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateTime = format.format(new Date());
        return sellerMapper.insertOneClerkRelationShip(id, clerkId,dateTime);
    }

    /**
     * 商户删除与店员的关系
     *
     * @param sellerId
     * @param clerkId
     */
    @Override
    public void deleteOneSellerClerk(String sellerId, String clerkId) {
        sellerMapper.deleteSellerClerk(sellerId, clerkId);
    }

    /**
     * 商户批量删除与店员的关系
     *
     * @param sellerId
     * @param clerkIds
     */
    @Override
    public void deleteListSellerClerk(String sellerId, List<String> clerkIds) {
        sellerMapper.deleteSellerClerks(sellerId, clerkIds);
    }

    /**
     * 获取该商户下的店员列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<SellerVo> getListOfClerk(String userId) {

        return sellerMapper.getListOfClerk(userId);
    }

    /**
     * 获取该商户下的店铺列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<ShopDTO> getListOfStores(String userId) {
        CommonResult<List<ShopDTO>> commonResult= shopService.getListOfStores(userId);
        List<ShopDTO> list=null;
        if (commonResult.isSuccess()){
            list=commonResult.getData();
        }
        return list;
    }

    /**
     * 通过id删除一个商户(逻辑删除，更新is_delete为1)
     *
     * @param id
     */
    @Override
    public void deleteOneSeller(String id) {
        sellerMapper.deleteSeller(id);
    }

    /**
     * 通过id批量删除商户
     *
     * @param asList
     */
    @Override
    public void deleteListSeller(List<String> asList) {
        sellerMapper.deleteSellers(asList);
    }

    @Override
    public void addSellerShopRelationShip(String id, String id1) {

        shopService.addSellerShopRelationShip(id, id1);

    }

    /**
     * 查询店员所属店铺列表
     *
     * @param sellerId
     * @return
     */
    @Override
    public List<ShopDTO> queryClerkBelongShops(String sellerId) {
        CommonResult<List<ShopDTO>> commonResult=shopService.queryClerkBelongShops(sellerId);
        if (commonResult.isSuccess()){
            return commonResult.getData();
        }
        return Collections.emptyList();
    }

    @Override
    public String getMobileById(String receiver) {
        return sellerMapper.getMobileById(receiver);
    }

    /**
     * 根据店铺id查询商户id
     * @param shopId
     * @return
     */
    @Override
    public SellerVo getSellerIdByShopId(String shopId) {
        return sellerMapper.getSellerIdByShopId(shopId);
    }

    @Override
    public List<SellerVo> getSellersFilterIsDeleteAndType(SellerVo vo, Integer pageNo, Integer pageSize) {
        int startPageNo = (pageNo - 1) * pageSize;
        List<SellerVo> sellers = sellerMapper.getSellersFilterIsDeleteAndType(vo, startPageNo, pageSize);
        return sellers;
    }


}
